package com.tedu.sp03.user.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tedu.sp01.pojo.User;
import com.tedu.sp01.service.UserService;
import com.tedu.sp01.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Value("${sp.user-service.users}")
    private String userJson;
    @Override
    public User getUser(Integer userId) {
        log.info("users json string:"+userJson);
        //本来应该是List<User>.Class 但是没这个写法，所以java里提供了一个类
        //new TypeReference<List<User>>获得这个类对象
        List<User> list= JsonUtil.from(userJson, new TypeReference<List<User>>() {});
        for(User u:list) {
            if (u.getId().equals(userId)) {
                return u;
            }
        }
        return new User(userId,"name-"+userId,"pwd-"+userId);
    }
    @Override
    public void addScore(Integer userId,Integer score) {
        //这里增加积分
        log.info("user"+userId+"-增加积分"+score);

    }
}
